package com.example.myapplication.widget;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.example.myapplication.R;
import com.example.myapplication.utils.Utils;

public class ButtonUsageActivity extends AppCompatActivity implements View.OnClickListener, View.OnLongClickListener {

    // 将文本框定义成类成员变量，在onCreate中只需要使用findViewById初始化一次就可以，不需要每次使用都通过findViewById查找
    private TextView text1;
    private TextView text2;
    private TextView text3;
    private Button button3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_button_usage);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        text1 = findViewById(R.id.text1);
        text2 = findViewById(R.id.text2);

        Button button1 = findViewById(R.id.button1);
        button1.setOnClickListener(new Button1OnClickListener(text1));

        Button button2 = findViewById(R.id.button2);
        button2.setOnClickListener(this);
        button2.setOnLongClickListener(this);

        button3 = findViewById(R.id.button3);
        button3.setOnClickListener(this);

        text3 = findViewById(R.id.text3);

        Button buttonEnable = findViewById(R.id.button_enable);
        buttonEnable.setOnClickListener(this);
        Button buttonDisable = findViewById(R.id.button_disable);
        buttonDisable.setOnClickListener(this);
    }



    @Override
    public void onClick(View v) {
        Log.d("ButtonUsageActivity", String.format("当前点击的是button2吗？%s", v.getId() == R.id.button2));
        if (v.getId() == R.id.button2) {
            String desc = String.format("%s 您点击了按钮：%s",
                    Utils.getFormattedCurrentTime(),
                    ((Button)v).getText());
            text2.setText(desc);
        } else if (v.getId() == R.id.button3) {
            String desc = String.format("%s 您点击了按钮：%s",
                    Utils.getFormattedCurrentTime(),
                    ((Button)v).getText());
            text3.setText(desc);
        } else if (v.getId() == R.id.button_enable) {
            if (!button3.isEnabled()) {
                button3.setEnabled(true);
                button3.setText("点击我查看当前时间");
            }
        } else if (v.getId() == R.id.button_disable) {
            if (button3.isEnabled()) {
                button3.setEnabled(false);
                button3.setText("我现在被禁用了，不能点击哦");
            }
        }
    }

    @Override
    public boolean onLongClick(View v) {
        if (v.getId() == R.id.button2) {
            String desc = String.format("%s 您长按了按钮：%s",
                    Utils.getFormattedCurrentTime(),
                    ((Button)v).getText());
            text2.setText(desc);
            return true;
        }
        return false;
    }

    /**
     * 定义静态内部类，防止内存泄漏。（非静态内部类的每个实例需要持有外部类的一个实例对象的引用）
     */
    static class Button1OnClickListener implements View.OnClickListener {
        private TextView text;

        public Button1OnClickListener(TextView text) {
            this.text = text;
        }

        @Override
        public void onClick(View v) {
            String desc = String.format("%s 您点击了按钮：%s",
                    Utils.getFormattedCurrentTime(),
                    ((Button)v).getText());
            text.setText(desc);
        }
    }
}